Who are you? (Share in chat or with your neighbor)
03:00 Apply tools for Tidying data to get a messy dataset into analysis-ready form, via data recoding, data transformations, and data subsetting.
Design and Create simple, custom functions that can be reused throughout an analysis on multiple datasets.
Explain and utilize iteration in programming to reduce repeated code and batch process collections (such as a folder of files or rows in a table)
At the end of the course, you will be able to: conduct a full analysis in the data science workflow (minus model).
We ask you to follow Participation Guidelines and Code of Conduct.
| Week | Date | Subject |
|---|---|---|
| 1 | Jan 22* | Fundamentals: vectors, data.frames, and lists |
| 2 | Jan 29 | Data Cleaning 1 |
| 3 | Feb 5 | Data Cleaning 2 |
| 4 | Feb 12* | Writing Functions |
| - | Feb 19 | No class - school week |
| 5 | Feb 26* | Iterating/Repeating Tasks |
| 6 | Mar 6* | Overflow/Celebratory Lunch |
*Ted on Campus
A pre-course survey:
https://forms.gle/4ouiHhP8Hbf25L9w5
05:00 classwork
Make a vector with the following values: 3, 5, 10. Assign it to an object called people. Show the contents of people.
NAvectordata.framelistA vector contains a data type, and all elements must be the same data type. We can have logical vectors, numerical vectors, etc.
Within the Numeric type that we are familiar with, there are more specific types: Integer vectors consists of whole number values, and Double vectors consists of decimal values.
We can test whether a vector is a certain type with is.___() functions, such as is.character().
[1] TRUE
We can coerce vectors from one type to the other with as.___() functions, such as as.numeric()
[1] 23 45
It is common to have metadata attributes, such as names, attached to R data structures.
a b c
1 2 3
attributes()We can look for more general attributes via the attributes() function:
$names
[1] "a" "b" "c"
Comparison operators, such as >, <=, ==, !=, create logical vectors for subsetting.
[1] FALSE FALSE TRUE TRUE FALSE TRUE FALSE
05:00 NA values?data.frameUsually, we load in a data.frame from a spreadsheet or a package.
data.frame attributesLet’s take a look at a data.frame’s attributes.
$class
[1] "tbl_df" "tbl" "data.frame"
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
[127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
[163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
[181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
[199] 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
[217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
[235] 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
[253] 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
[271] 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
[289] 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
[307] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
[325] 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
[343] 343 344
$names
[1] "species" "island" "bill_length_mm"
[4] "bill_depth_mm" "flipper_length_mm" "body_mass_g"
[7] "sex" "year"
data.frames 1 (5 minutes, go as far as you can)05:00 Subset to the single column bill_length_mm:
data.frames 2I want to select columns bill_length_mm, bill_depth_mm, species, and filter the rows so that species only has “Gentoo”:
data.frames 3Challenge: I want to filter out rows that have NAs in the column bill_length_mm:
Lists operate similarly as vectors as they group data into one dimension, but each element of a list can be any data type or data structure!
Unlike vectors, you access the elements of a list via the double bracket [[]]. (You will access a smaller list with single bracket [].)
[1] 1 2 3
We can give names to lists:
And access named elements of lists via the [[]] or $ operation:
Therefore, l1$score is the same as l1[[4]] and is the same as l1[["score"]].
What data structure does this remind you of?
[] versus [[]]This always trips me up, you usually want [[]] (return an element) versus [] (returns a sublist).
$ranking
[1] 1 2 3
…
[1] 1 2 3
lm() - a lot of methods in R use this.list of data.frames05:00 Return the element in the id slot:
Return the 2nd element of this list:
How would you use the value of the my_col variable to subset the list?
data.frames as ListsA data.frame is just a named list of vectors of same length with attributes of (column) names and row.names, so all of the list methods we looked at above apply.
data.frames as Listslapply() function - applies a function to each element of a list{purrr} package, which has methods for working with listsMaybe see you Friday 10 - 11 AM PST to practice together!